package com.guogee.pdb.dao;

import com.guogee.pdb.bean.FamilyDeviceHistory;
import com.guogee.pdb.db.DBHelper.FamilyDevicesHistoryCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class FamilyDeviceHistoryDAO extends ADao<FamilyDeviceHistory> {
	public FamilyDeviceHistoryDAO() {
		dbCollection = dbHelper.getIsmartDB().getCollection(FamilyDevicesHistoryCollection.COLLECTION_NAME);
		if(dbCollection.getIndexInfo() == null){
			dbCollection.createIndex(new BasicDBObject(FamilyDevicesHistoryCollection.FAMILY_ID,-1));
		}
	}
	@Override
	public boolean insertItem(FamilyDeviceHistory obj) {
		// TODO Auto-generated method stub
		if(insertItem1(obj)!=null){
			return true;
		}
		return false;
	}

	@Override
	public String insertItem1(FamilyDeviceHistory obj) {
		// TODO Auto-generated method stub
		DBObject dbObj = new BasicDBObject();
		dbObj.put(FamilyDevicesHistoryCollection.FAMILY_ID, obj.getFamilyId());
		dbObj.put(FamilyDevicesHistoryCollection.DEV_MAC, obj.getDevMac());
		dbObj.put(FamilyDevicesHistoryCollection.DEV_TYPE, obj.getDevType());
		dbObj.put(FamilyDevicesHistoryCollection.DEV_VERSION, obj.getDevVersion());
		dbObj.put(FamilyDevicesHistoryCollection.BIND_TIME, obj.getBindTime());
		if(checkWritenResult(dbCollection.insert(dbObj))){
			return dbObj.get(FamilyDevicesHistoryCollection.ID).toString();
		}
		return null;
	}

	@Override
	public FamilyDeviceHistory parstItemFromCursor(DBCursor cursor) {
		// TODO Auto-generated method stub
		return parstDBObject(cursor.next());
	}

	@Override
	public FamilyDeviceHistory parstDBObject(DBObject dbObj) {
		// TODO Auto-generated method stub
		FamilyDeviceHistory obj = new FamilyDeviceHistory();
		obj.setId(dbObj.get(FamilyDevicesHistoryCollection.ID).toString());
		obj.setFamilyId(dbObj.get(FamilyDevicesHistoryCollection.FAMILY_ID).toString());
		obj.setDevMac(dbObj.get(FamilyDevicesHistoryCollection.DEV_MAC).toString());
		obj.setDevType(Short.parseShort(dbObj.get(FamilyDevicesHistoryCollection.DEV_TYPE).toString()));
		obj.setDevVersion(Short.parseShort(dbObj.get(FamilyDevicesHistoryCollection.DEV_VERSION).toString()));
		obj.setBindTime(Long.parseLong(dbObj.get(FamilyDevicesHistoryCollection.BIND_TIME).toString()));
		return obj;
	}

}
